<<<<<<< HEAD:cxx/algorithm/src/divide.cc
#include "divide.h"
// int divide(int dividend, int divisor)
// {
//     long int res = 0;
//     if (divisor == 0)
//         return 0;
//     int MIN_VALUE = (numeric_limits<int>::min)();
//     int MAX_VALUE = (numeric_limits<int>::max)();
//     bool neg = dividend >= 0 ? false : true;
//     bool neg_v = divisor >= 0 ? false : true;

//     long int dividend_l = static_cast<long int>(dividend);
//     long int divisor_l = static_cast<long int>(divisor);

//     dividend_l = dividend_l < 0 ? -dividend_l : dividend_l;
//     divisor_l = divisor_l < 0 ? -divisor_l : divisor_l;

//     for (int i = 30; i >= 0; i--)
//     {
//         int bitValue = dividend_l << i;
//         if (residual < divisor_l)
//         {
//             res += i;
//             dividend_l -= residual;
//         }
//         else
//             continue;
//     }
//     if (res > MAX_VALUE)
//         res = MAX_VALUE;
//     if (res < MIN_VALUE)
//         res = MIN_VALUE;
//     res = static_cast<int>(res);
//     return (neg == neg_v) ? res : -res;
// }
=======
#include "common.h"
namespace common {
int div(long dividend, long divisor) { // 对正常正整数的除法逻辑
    if (dividend < divisor)
        return 0;
    long count = 1;
    long td = divisor;
    while ((td + td) <= dividend) {
        count = count << 1;
        td = td << 1;
    }
    return count + div(dividend - td, divisor);
}
int divide(int dividend, int divisor) {
    if (dividend == 0)
        return 0;
    if (divisor == 1)
        return dividend;
    if (divisor == -1) {
        if (dividend > std::numeric_limits<int>::min())
            return -dividend; // 只要不是最小的那个整数，都是直接返回相反数就好啦
        return std::numeric_limits<int>::max(); // 最小值除以1导致溢出
    }
    long a = dividend;
    long b = divisor;
    int sign = 1;
    if ((a > 0 && b < 0) || (a < 0 && b > 0)) {
        sign = -1;
    }
    a = a > 0 ? a : -a;
    b = b > 0 ? b : -b;
    long res = div(a, b);
    if (sign > 0)
        return res > std::numeric_limits<int>::max()
                   ? std::numeric_limits<int>::max()
                   : res;
    else
        return -res;
}
} // namespace common
>>>>>>> dev:cxx/algorithm/common/src/divide.cc
